<?php
error_reporting(E_ALL ^ E_NOTICE);
$URL = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$URL = urldecode ($URL);
$URL = str_replace ('&amp;', '&', $URL);
 
//Check if PHP is not in safe mode,
//and PHPSESSID is passed via URL
if (preg_match ('#'.session_name().'=([^=&\s]*)#i', $URL))
{
   //Remove PHPSESSID junk and unneeded characters ("&" or "?") at end of URL
   $URL = preg_replace ( array ('#(\?|&)'.session_name().'=([^=&\s]*)#', '#(&|\?)+$#'), '', $URL);
   header ("HTTP/1.1 301 Moved Permanently");
   header ("Location: " . trim ($URL));
   exit();
}

function getMenuFromID($over_kategori)
{
	global $database;
	$MenuQuery	= sprintf("SELECT * FROM kategori WHERE over_kategori = %s", $over_kategori);
	return $database -> mysql_get_array( $MenuQuery );
}
ob_start();

include( '../config.php' );
include($_CONFIG['paths']['lib'].'class.database.php');
include($_CONFIG['paths']['lib'].'class.form.php');
include($_CONFIG['paths']['lib'].'class.mailer.php');
include($_CONFIG['paths']['lib'].'class.session.php');
include($_CONFIG['paths']['lib'].'class.cart.php');
include($_CONFIG['paths']['lib'].'class.bread.php');
include($_CONFIG['paths']['lib'].'class.url.php');
include($_CONFIG['paths']['lib'].'func.php');

$database = new DB();
$s        = new Session();
$cart     = new Cart();
$bread    = new Bread();
$url      = new Url();
$url 	  -> getUrl();

$rJs      = Array();
$rCss     = Array();
$rForm    = Array();
$rJs[] 	= 'productList.js';
$rJs[] 	= 'stripe.js';
$rJs[] 	= 'cart.js';
$rJs[] 	= 'form.js';
#$rJs[] 	= 'sifr.js';
#$rJs[] 	= 'sifr-config.js';
#$rCss[]	= 'main.css';
$rCss[]		= 'form.css';
$rCss[]		= '1.css';
#$rCss[]	= 'sifr.css';
/*
print_r($url -> urlSplit);
print '<br>urlSection : ';
print_r($url->urlSection);
print '<br>urlPage : ';
print_r($url->urlPage);
print '<br>urlData : ';
print_r($url->urlData);
print '<br>urlKategori : ';
print_r($url->urlKategori);
print '<br>urlProduct : ';
print_r($url->urlProduct);
print '<br>urlAction : '.$url->urlAction;
print '<br>urlVar : ';
print_r($url->urlVar);
print '<br>url404 : '.$url->url404;
print '<br>bread : ';
print $bread->getBread();
print '<br><br>'.$bread->getUrl();
*/
$rRequest['section'] 	= $url->urlSection;
$rRequest['page'] 		= $url->urlPage;
$rRequest['kategori'] 	= $url->urlKategori;
$rRequest['product'] 	= $url->urlProduct;
$rRequest['data'] 		= $url->urlData;
$rRequest['action'] 	= $url->urlAction;
$rRequest['var'] 		= $url->urlVar;
$rRequest['404'] 		= $url->url404;

$get = $rRequest['var'];
$primaryMenu = getMenuFromID(0);
$uMenu = getMenuFromID ( $rRequest['section'][0] );

// Inkludera bara en action-fil ifall $rRequest['action'] är satt och innehåller en fil
if( isset( $rRequest['action'] ) )
{
    include( 
        $_CONFIG['paths']['data']  . 
        $rRequest['section'][1] . 
        '/'.'action.'. basename( $rRequest['action'] ) .'.php' 
    );
}

// Inkludera bara en data-fil ifall $rRequest['data'] är satt och innehåller en fil
if( $rRequest['data']  )
{
    include( 
        $_CONFIG['paths']['data']  . 
        $rRequest['section'][1] . 
        '/'.'data.'. $rRequest['page'][1] .'.php' 
    );
}

if ( !$rResponse['no_template'] )
{
	// Inkludera template-filen
	include( 
		 $_CONFIG['paths']['tpl'] . 
		 $rRequest['section'][1] . 
		 '/'.'tpl.'. $rRequest['page'][1] .'.php' 
	);
	// Eftersom vi slagit på output bufferten kan vi spara allt från template-filen
	// i en variabel, som main.tpl.php använder
			 #print '<h1>'.$urlPage.' -- '.$rRequest['page'].' -- '.isMenu( $urlPage ).' --</h1>';


	$rResponse['contents'] = ob_get_clean();
	#print $rResponse['contents'];
	#print $rRequest['head_content'];
	include( $_CONFIG['paths']['tpl'] . 'main.tpl.php' );
}
?>